package com.itheima.reggie.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*@Component
@WebFilter(filterName="LoginCheckFilter",urlPatterns="/*")*/
@Slf4j

public class LoginCheckFilter implements Filter {
    public void init(FilterConfig config) throws ServletException {
        log.info("初始化过滤器。。。");
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletResponse httpResponse = (HttpServletResponse) response;

        httpResponse.setContentType("text/html;charset=utf-8");
        String remoteAddr = request.getRemoteAddr();

        //阻止 非本机的 管理员登录
        if (!(remoteAddr.equals("0:0:0:0:0:0:0:1" ) || remoteAddr.equals("192.168.43.1" ) )){
            log.info("已过滤请求: \n 请求地址:{} " ,remoteAddr);
            log.info("用户未授权");
            httpResponse.sendRedirect("http://localhost:8080/backend/page/login/login.html");
//            response.sendRedirect("http://localhost:8080/backend/page/index.html");
            return;
        }
        chain.doFilter(request, response);
    }
}
